package com.joesem.sale.client.base

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.BarUtils
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.jiaxing.sale.client.R
import java.lang.reflect.ParameterizedType


abstract class BaseBottomFragmentDialog<Bind : ViewBinding> : BottomSheetDialogFragment(), UiImpl {

    open lateinit var bind: Bind
    private var topOffset = 0
    private var behavior: BottomSheetBehavior<FrameLayout>? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return initBind(inflater, container, savedInstanceState)?.root
    }

    private fun initBind(
        inflater: LayoutInflater,
        container: ViewGroup?, savedInstanceState: Bundle?,
    ): Bind? {
        try {
            this@BaseBottomFragmentDialog::class.java.genericSuperclass.let { it ->
                val type = ((it as ParameterizedType).actualTypeArguments[0]) as Class<*>
                type.getDeclaredMethod(
                    "inflate",
                    LayoutInflater::class.java,
                    ViewGroup::class.java,
                    Boolean::class.java
                ).let {
                    bind = it.invoke(null, inflater, container, false) as Bind
                    bind.initView(savedInstanceState)
                    bind.initListener()
                    return bind
                }
            }
        } catch (e: Exception) {
            return null
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (dialog is BottomSheetDialog) {
            val behaviour = (dialog as BottomSheetDialog).behavior
            behaviour.state = BottomSheetBehavior.STATE_EXPANDED
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

    protected open fun contentMax(): Int {
        val height = requireContext().resources.displayMetrics.heightPixels
        return height - (BarUtils.getStatusBarHeight() * 2)
    }

    override fun onStart() {
        super.onStart()
        dialog?.let {
            if (it is BottomSheetDialog) {
                it.delegate.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
                    ?.let { bottomSheet ->
                        val layoutP = bottomSheet.layoutParams as (CoordinatorLayout.LayoutParams)
                        layoutP.height = contentMax()
                        bottomSheet.layoutParams = layoutP
                        BottomSheetBehavior.from(bottomSheet).apply {
                            peekHeight = contentMax()
                            state = BottomSheetBehavior.STATE_EXPANDED
                            isHideable = false

                        }
                    }

            }
        }
    }
    open fun setTopOffset(topOffset: Int) {
        this.topOffset = topOffset
    }

    protected abstract fun Bind.initView(savedInstanceState: Bundle?)

    protected open fun Bind.initListener() {

    }

    protected open fun Bind.initIntent(intent: Any?) {

    }

    override fun observerIntent(intent: Any?) {
        bind.initIntent(intent)
    }

    override fun showLoading() {

    }

    override fun dismissLoading() {

    }


}